//
//  PeopleListViewController.m
//  AllFamily
//
//  Created by chirui on 2018/5/30.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "PeopleListViewController.h"
#import "PeopleTableViewCell.h"
#import "AddPeopleHelpViewController.h"
#import "HelpPayForViewController.h"

@interface PeopleListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    UIView * bgView;
    NSString * messageUrl;
}

@end

@implementation PeopleListViewController


-(void)viewWillAppear:(BOOL)animated {
    
     [self getUserListData];
    [self getData];
}





- (void)viewDidLoad {
    [super viewDidLoad];
  
    TitleView(@"健康值账户");
    [self creatTableView];
    
    
   
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
    [self creatEndView];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData {
    NSDictionary * dic = @{@"type":@"help_introduction"};
    [PostNetWorking postNetWorkingStr:[address_url stringByAppendingString:user_agreement_api] andparameters:dic Success:^(id responseObject) {
        if([responseObject[@"code"]intValue] == 200) {
            messageUrl = responseObject[@"datas"][@"url"];
            
        }
        
    } failed:^(NSError *error) {
        [MBProgressHUD showError:@"网络错误" toView:self.view];
    }];
}

-(void)creatWindow{
    
    
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bgView.backgroundColor = [[UIColor alloc]initWithRed:0 green:0 blue:0 alpha:0.5];
    [window addSubview:bgView];
    
    UIView * whitView = [[UIView alloc]initWithFrame:CGRectMake(30, (HEIGHT-250)/2, WIDTH-60, 250)];
    whitView.backgroundColor = [UIColor whiteColor];
    whitView.layer.cornerRadius = 5.f;
    whitView.layer.masksToBounds = YES;
    [bgView addSubview:whitView];
    
    
    
    UIWebView * web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH-60, 200)];
     [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:messageUrl]]];
    web.backgroundColor = [UIColor whiteColor];
    [whitView addSubview:web];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(30, 210, 40, 30);
    
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(livecancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [whitView addSubview:cancelBtn];
    
    UIButton * surelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    surelBtn.frame = CGRectMake(WIDTH -60 -70, 210, 40, 30);
    
    [surelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [surelBtn addTarget:self action:@selector(livesureBtn:) forControlEvents:UIControlEventTouchUpInside];
    [surelBtn setTitle:@"确定" forState:UIControlStateNormal];
    surelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [whitView addSubview:surelBtn];
    
}

-(void)livecancelBtn:(UIButton *)btn{
    
    [bgView removeFromSuperview];
}

-(void)livesureBtn:(UIButton *)btn{
    
     [bgView removeFromSuperview];
    AddPeopleHelpViewController * add = [[AddPeopleHelpViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

-(void)creatEndView{
    
    UIView * endView =[[UIView alloc]init];
    
    if (ISPX) {
        endView.frame = CGRectMake(0, HEIGHT-64-48-34, WIDTH, 48+32);
    }else{
        endView.frame = CGRectMake(0, HEIGHT-64-48, WIDTH, 48);
    }
    endView.backgroundColor = NavBackColor;
    
    UIButton * endBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(0, 0, WIDTH, 48);
    endBtn.backgroundColor = NavBackColor;
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [endBtn setTitle:@"添加人员" forState:UIControlStateNormal];
    [endBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [endView addSubview:endBtn];
    [self.view addSubview:endView];
}

-(void)addBtnClick{
    
    if (messageUrl==nil||messageUrl.length<=0) {
        
        [self getData];
    }else {
        
        [self creatWindow];
    }
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight-48) style:UITableViewStyleGrouped];
    if (ISPX) {
        _tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight-48-32);
    }else{
        _tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight-48);
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[PeopleTableViewCell reuseIdentifier ] bundle:nil] forCellReuseIdentifier:[PeopleTableViewCell reuseIdentifier]];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    PeopleTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[PeopleTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    cell.nameLabel.text = dic[@"name"];
    cell.healthLabel.attributedText = [SHJudgeTools changeAllStr:[NSString stringWithFormat:@"健康值  %@",dic[@"balance"]] partStr:[NSString stringWithFormat:@"%@",dic[@"balance"]] color:[UIColor greenColor] font:14];
    cell.cardNumberLabel.text = dic[@"id_card_num"];
    cell.topUpLabel.layer.cornerRadius = 5.f;
    cell.topUpLabel.layer.masksToBounds = YES;
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    heardView.backgroundColor = [UIColor whiteColor];
    UILabel * titlelabel  =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    titlelabel.text = @"  健康值账户列表";
    titlelabel.font = [UIFont systemFontOfSize:17];
    [heardView addSubview:titlelabel];
    return heardView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    HelpPayForViewController * help = [[HelpPayForViewController alloc]init];
    help.dataDic = dic;
    [self.navigationController pushViewController:help animated:YES];
    
}

-(void)getUserListData{
    
 
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,get_memberList_api];
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:parametersStr andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
          
                if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {

                        _dataArray  = responseObject[@"datas"];
                }
            [_tableView reloadData];
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


@end
